package com.aviccxzx.smarthome.utils;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.netutil.app.App;


/**
 * Created by joey on 2016/10/27.
 */
public class ChangeNameDialogUtil {
    private Context mContext;
    private int dialogWidth;
    private Dialog dialog;
    private EditText name;
    public SureInterfance listener;
    private String titleName;
    private String mContent;
    public interface SureInterfance{
        public void sureTodo(String content);
    }

    public ChangeNameDialogUtil(Context mContext) {
        this.mContext = mContext;
    }

    public ChangeNameDialogUtil(Context mContext,String txt_title, final SureInterfance listener) {
        this.mContext = mContext;
        this.listener = listener;
        this.titleName = txt_title;
    }

    public ChangeNameDialogUtil(Context mContext,String txt_title, String content,final SureInterfance listener) {
        this.mContext = mContext;
        this.listener = listener;
        this.titleName = txt_title;
        this.mContent = content;
    }


    public void showSelfDialog() {
        final EditText name;
        if(dialog == null){
            dialog = new Dialog(mContext);
            View dView = View.inflate(mContext, R.layout.change_name_dialog,null);
            dialog.setContentView(dView);

            TextView ok= (TextView) dView.findViewById(R.id.btn_sure);
            TextView txt_title= (TextView) dView.findViewById(R.id.txt_title);
            TextView cancle = (TextView) dView.findViewById(R.id.btn_cancle);
            name = (EditText) dView.findViewById(R.id.edit_name);
            if (!TextUtils.isEmpty(mContent)){
                name.setText(mContent);
            }
            int width = WindowManager.LayoutParams.MATCH_PARENT;
            txt_title.setText(titleName);
            dialog.getWindow().setLayout(width,WindowManager.LayoutParams.WRAP_CONTENT);
            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String all = "^[a-zA-Z0-9\\u4e00-\\u9fa5]+$";
                    String mName = name.getText().toString();
                    if (!TextUtils.isEmpty(mName)&&mName.matches(all)){
                        listener.sureTodo(mName);
                        dialog.dismiss();
                    }else {
                        App.getApp().showToast("只能输入汉字、字母或数字");
                    }
                }
            });
            cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(dialog.isShowing())
                    dialog.dismiss();
                }
            });
        }
        dialog.show();
    }
}
